package com.yanqu.road.entity.union.trade;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.enums.union.trade.eUnionTradeRouteState;
import com.yanqu.road.entity.player.UserBaseInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 商会贸易（商贸）队伍
 */
public class UnionTradeTeamData extends DataObject {

    private String unionUid;                   //商会id

    private int routeId;                       //路线id

    private long openUser;                     //开启玩家

    private long openTime;                     //开启时间

    private long  startTradeTime;              //开始商贸时间（秒）

    private int stateVal;                      //状态（未出发；进行中；已结束）

    private String expectReward;                       //预计奖励

    private int  finishType;                           //出发商贸结束的类型（1：正常完成；2：退出商会；3：解散商会；)

    private String reward;                             //商贸奖励

    private String eventReward;                        //事件奖励

    private boolean sendReward;                        //是否已发奖

    private List<UnionTradeLog> logDataList = new ArrayList<>();                   //路线日志

    private List<UnionTradeTeamUserJoin> userJoinDataList = new ArrayList<>();     //玩家参与信息

    private List<UnionTradeEventRead> userReadList = new ArrayList<>();            //玩家读取事件信息

    private List<Integer> eventTimeList = new ArrayList<>();                        //随机时间点

    private List<Integer> eventList = new ArrayList<>();                            //随机事件ID

    private int takeEventIndex = -1;                                                //已发生事件索引

    private int openNum;                               //开启次数

    private long restTime;                             //重置时间

    private List<UserBaseInfo>  userInfoList = new ArrayList<>();        //玩家详情

    //配置
    private long condition;

    //配置
    private String newRewards;

    public String getUnionUid() {
        return unionUid;
    }

    public void setUnionUid(String unionUid) {
        if (unionUid == null) {
            return;
        }
        if (!unionUid.equals(this.unionUid)) {
            this.unionUid = unionUid;
            setUpdateOption();
        }
    }

    public int getRouteId() {
        return routeId;
    }

    public void setRouteId(int routeId) {
        if(this.routeId != routeId){
            this.routeId = routeId;
            setUpdateOption();
        }
    }

    public long getOpenUser() {
        return openUser;
    }

    public void setOpenUser(long openUser) {
        if(this.openUser != openUser){
            this.openUser = openUser;
            setUpdateOption();
        }
    }

    public long getOpenTime() {
        return openTime;
    }

    public void setOpenTime(long openTime) {
        if(this.openTime != openTime) {
            this.openTime = openTime;
            setUpdateOption();
        }
    }

    public long getStartTradeTime() {
        return startTradeTime;
    }

    public void setStartTradeTime(long startTradeTime) {
        if(this.startTradeTime != startTradeTime){
            this.startTradeTime = startTradeTime;
            setUpdateOption();
        }
    }

    public int getStateVal() {
        return stateVal;
    }

    public void setStateVal(int stateVal) {
        if(this.stateVal != stateVal){
            this.stateVal = stateVal;
            setUpdateOption();
        }
    }

    public String getExpectReward() {
        return expectReward;
    }

    public void setExpectReward(String expectReward) {
        if(expectReward == null){
            return;
        }
        if(!expectReward.equals(this.expectReward )) {
            this.expectReward = expectReward;
            setUpdateOption();
        }
    }

    public int getFinishType() {
        return finishType;
    }

    public void setFinishType(int finishType) {
        if(this.finishType != finishType) {
            this.finishType = finishType;
            setUpdateOption();
        }
    }

    public String getReward() {
        return reward;
    }

    public void setReward(String reward) {
        if(reward == null){
            return;
        }
        if(!reward.equals(this.reward)) {
            this.reward = reward;
            setUpdateOption();
        }
    }

    public String getEventReward() {
        return eventReward;
    }

    public void setEventReward(String eventReward) {
        if(eventReward == null){
            return;
        }
        if(!eventReward.equals(this.eventReward)) {
            this.eventReward = eventReward;
            setUpdateOption();
        }
    }

    public boolean isSendReward() {
        return sendReward;
    }

    public void setSendReward(boolean sendReward) {
        if(this.sendReward != sendReward) {
            this.sendReward = sendReward;
            setUpdateOption();
        }
    }

    public List<UnionTradeLog> getLogDataList() {
        return logDataList;
    }

    public List<UnionTradeLog> getNewLogDataList() {
        synchronized (this) {
            return new ArrayList<>(logDataList);
        }
    }

    public void setLogDataList(List<UnionTradeLog> logDataList) {
        this.logDataList = logDataList;
        setUpdateOption();
    }

    public void addLogData(UnionTradeLog logData) {
        this.logDataList.add(logData);
        setUpdateOption();
    }

    public List<UnionTradeTeamUserJoin> getUserJoinDataList() {
        return userJoinDataList;
    }

    public List<UnionTradeTeamUserJoin> getNewUserJoinDataList() {
        synchronized (this) {
            return new ArrayList<>(userJoinDataList);
        }
    }

    public void setUserJoinDataList(List<UnionTradeTeamUserJoin> userJoinDataList) {
        this.userJoinDataList = userJoinDataList;
        setUpdateOption();
    }

    public void addAllJoinData(List<UnionTradeTeamUserJoin> userJoinDataList){
        this.userJoinDataList.addAll(userJoinDataList);
        setUpdateOption();
    }

    public List<UnionTradeEventRead> getUserReadList() {
        return userReadList;
    }

    public List<UnionTradeEventRead> getNewUserReadList() {
        synchronized (this) {
            return new ArrayList<>(userReadList);
        }
    }

    public void setUserReadList(List<UnionTradeEventRead> userReadList) {
        this.userReadList = userReadList;
        setUpdateOption();
    }

    public List<Integer> getEventTimeList() {
        return eventTimeList;
    }

    public void setEventTimeList(List<Integer> eventTimeList) {
        this.eventTimeList = eventTimeList;
        this.setUpdateOption();
    }

    public void addEventTime(List<Integer> eventTime){
        this.eventTimeList.addAll(eventTime);
        this.setUpdateOption();
    }

    public List<Integer> getEventList() {
        return eventList;
    }

    public void setEventList(List<Integer> eventList) {
        this.eventList = eventList;
        setUpdateOption();
    }

    public void addEventList(List<Integer> eventList){
        this.eventList.addAll(eventList);
        setUpdateOption();
    }

    public int getTakeEventIndex() {
        return takeEventIndex;
    }

    public void setTakeEventIndex(int takeEventIndex) {
        if(this.takeEventIndex != takeEventIndex) {
            this.takeEventIndex = takeEventIndex;
            setUpdateOption();
        }
    }

    public int getOpenNum() {
        return openNum;
    }

    public void setOpenNum(int openNum) {
        if(this.openNum != openNum) {
            this.openNum = openNum;
        }
    }

    public long getRestTime() {
        return restTime;
    }

    public void setRestTime(long restTime) {
        if(this.restTime != restTime) {
            this.restTime = restTime;
        }
    }

    public List<UserBaseInfo> getUserInfoList() {
        return userInfoList;
    }

    public List<UserBaseInfo> getNewUserInfoList() {
        synchronized (this){
            return new ArrayList<>(userInfoList);
        }
    }

    public void setUserInfoList(List<UserBaseInfo> userInfoList) {
        this.userInfoList = userInfoList;
        setUpdateOption();
    }

    public void addUserInfo(UserBaseInfo userBaseInfo){
        if(!this.containsUser(userBaseInfo.getUserAliasId())) {
            this.userInfoList.add(userBaseInfo);
            setUpdateOption();
        }
    }

    private boolean containsUser(long userAliasId){
        for(UserBaseInfo userBaseInfo : this.userInfoList){
            if(userBaseInfo.getUserAliasId() == userAliasId){
                return true;
            }
        }
        return false;
    }

    public long getCondition() {
        return condition;
    }

    public void setCondition(long condition) {
        if(this.condition != condition){
            this.condition = condition;
            setUpdateOption();
        }
    }

    public String getNewRewards() {
        return newRewards;
    }

    public void setNewRewards(String newRewards) {
        if(!Objects.equals(this.newRewards, newRewards)) {
            this.newRewards = newRewards;
            setUpdateOption();
        }
    }

    public void reset(){
        this.setOpenUser(0L);
        this.setOpenTime(0L);
        this.setStartTradeTime(0L);
        this.setStateVal(eUnionTradeRouteState.UnOpen.getIntValue());

        this.setExpectReward("");
        this.setFinishType(0);
        this.setReward("");
        this.setEventReward("");
        this.setSendReward(false);

        this.setLogDataList(new ArrayList<>());
        this.setUserJoinDataList(new ArrayList<>());
        this.setUserReadList(new ArrayList<>());

        this.setEventList(new ArrayList<>());
        this.setEventTimeList(new ArrayList<>());
        this.setTakeEventIndex(-1);

        this.setUserInfoList(new ArrayList<>());
        this.setCondition(0);
        this.setNewRewards("");
    }
}
